<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="!crud.props.searchToggle">
        <el-form size="small" label-width="120px" v-formkey="{query:crud.toQuery}">
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="业务日期">
                <div class="flex flex-row">
                  <el-date-picker
                    v-model="query.startTime"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="开始日期"
                  />
                  <el-date-picker
                    v-model="query.endTime"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="结束日期"
                  />
                </div>
              </el-form-item>
            </el-col>
              <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                <el-form-item label="来源单据审核日期" label-width="150px">
                  <div class="flex flex-row">
                    <el-date-picker
                      v-model="query.sourceOrderAuditStartTime"
                      align="right"
                      unlink-panels
                      class="fl1"
                      format="yyyy - MM - dd "
                      value-format="yyyy-MM-dd"
                      default-value
                      range-separator=""
                      placeholder="开始日期"
                    />
                    <el-date-picker
                      v-model="query.sourceOrderAuditEndTime"
                      align="right"
                      unlink-panels
                      class="fl1"
                      format="yyyy - MM - dd "
                      value-format="yyyy-MM-dd"
                      default-value
                      range-separator=""
                      placeholder="结束日期"
                    />
                  </div>
                </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="来源单据制单日期" label-width="150px">
                <div class="flex flex-row">
                  <el-date-picker
                    v-model="query.sourceOrderCreateStartTime"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="开始日期"
                  />
                  <el-date-picker
                    v-model="query.sourceOrderCreateEndTime"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="结束日期"
                  />
                </div>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="来源单据制单人" label-width="120px">
                <zSelect :value.sync="query.sourceOrderCreateUserId" :showName="crud.order.auditUserName"
                         url="api/dropDown/sysUserList" style="width:  100%;"
                />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="来源单据审核人" label-width="120px">
                <zSelect :value.sync="query.sourceOrderAuditUserId" :showName="crud.order.auditUserName"
                         url="api/dropDown/sysUserList" style="width:  100%;"
                />
              </el-form-item>
            </el-col>
            <el-col span="6">
              <el-form-item label="二批">
                <!--                <zSelect multiple :value.sync="customerIds"  url="api/dropDown/basCustomerList"-->
                <!--                         style="width:  100%;"/>-->
                <table-select
                  :tableColumns="
                  [
                  {prop:'code',label:'二批编码',width: '100'},
                  {prop:'name',label:'二批名称',width: '150'},
                  ]"
                  labelKey="name"
                  valueKey="id"
                  width="400"
                  :params="{type:3}"
                  :value.sync="query.sCustomerIds"
                  placeholder="请选择二批"
                  url="api/dropDown/basCustomerList"
                ></table-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="往来单位">
                <zSelect :value.sync="query.customerId" :label="query.customerName"
                         url="api/dropDown/basCustomerList" style="width: 100%;"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="银行">
                <table-select
                  :tableColumns="[{prop:'name',label:'名称',width: '280'}]"
                  :value.sync="query.bankIds"
                  placeholder="请选择银行"
                  url="api/basBank"
                  style="width:100%;"
                  :params="{sort: 'seq desc'}"
                ></table-select>
<!--                <zSelect :value.sync="query.bankIds"-->
<!--                         multiple-->
<!--                         url="api/basBank"-->
<!--                         style="width:100%;"-->
<!--                         :params="{sort: 'seq desc'}"-->
<!--                />-->
<!--                <zSelect :value.sync="query.bankIds"-->
<!--                         :multiple="true"-->
<!--                         url="api/basBank/getBasBanksById"-->
<!--                         :labelKey="'bankName'"-->
<!--                         :valueKey="'bankId'"-->
<!--                         style="width:100%;"-->
<!--                         :params="{sort: 'seq desc'}"-->
<!--                />-->
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="来源单据类型">
                <el-select v-model="query.sourceType" class="filter-item" clearable placeholder="请选择单据类型" size="small"
                           style="width: 100%;">
                  <el-option
                    v-for="item in dict.t_finance_bank_balance_detail_source_type"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="来源单号">
                <el-input v-model="query.sourceCode" placeholder="请输入来源单号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="状态">
                <el-select v-model="query.sourceOrderStatus" class="filter-item" clearable placeholder="状态"
                           size="small" style="width: 100%;"
                >
                  <el-option
                    v-for="item in dict.common_order_status"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" align="center">
              <rrOperation :crud="crud"/>
            </el-col>
          </el-row>
        </el-form>
        <el-button
        v-if="crud.optShow.download"
        :loading="crud.downloadLoading"
        class="filter-item"
        size="mini"
        type="warning"
        @click="crud.doExport"
      >
        导出
      </el-button>
      </div>
      <!--表格渲染-->
      <el-table ref="table"
                @row-click="crud.selectRow"
                :row-style="crud.rowClass"
                :row-class-name="crud.tableRowClassName"
                border v-loading="crud.loading" :data="crud.data" size="small" height="550" style="width: 100%;"
                show-summary
                :summary-method="sumRow"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column :render-header="crud.renderHeader" type="selection" width="60"/>
        <el-table-column :render-header="crud.renderHeader" type="index" width="60" label="序号"/>
        <el-table-column width="120" :render-header="crud.renderHeader" v-if="columns.visible('busDate')" prop="busDate"
                         label="业务日期">
          <template slot-scope="scope">
            <span>{{ parseDate(scope.row.busDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column width="160" :render-header="crud.renderHeader" v-if="columns.visible('sourceCode')"
                         prop="sourceCode"
                         label="来源单据编号">
          <template slot-scope="scope">
            <span style="color:#409eff;cursor: pointer;text-decoration: #409eff"  @click="goToPage(scope.row.sourceType,scope.row.sourceId)">{{ scope.row.sourceCode }}</span>
          </template>
        </el-table-column>
        <el-table-column width="100" :render-header="crud.renderHeader" v-if="columns.visible('sourceType')"
                         prop="sourceType"
                         label="来源类型">
          <template slot-scope="scope">
            <span v-for="item in dict.t_finance_bank_balance_detail_source_type"
                  v-if="scope.row.sourceType == item.value">
              {{ item.label }}
            </span>
          </template>
        </el-table-column>
        <el-table-column width="160" :render-header="crud.renderHeader" v-if="columns.visible('customerCode')"
                         prop="customerCode"
                         label="往来单位编码"/>
        <el-table-column width="160" :render-header="crud.renderHeader" v-if="columns.visible('customerName')"
                         prop="customerName"
                         label="往来单位"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('sCustomerName')" prop="sCustomerName"
                         label="所属二批"/>
        <el-table-column width="160" :render-header="crud.renderHeader" v-if="columns.visible('bankName')"
                         prop="bankName"
                         label="银行名称"/>
        <el-table-column width="160" :render-header="crud.renderHeader" v-if="columns.visible('inAmount')"
                         prop="inAmount"
                         label="金额"/>
        <el-table-column  :render-header="crud.renderHeader" v-if="columns.visible('sourceOrderCancelAmount')" prop="sourceOrderCancelAmount"
                         label="已核销金额"
        />
        <el-table-column  :render-header="crud.renderHeader" v-if="columns.visible('sourceOrderUnCancelAmount')"
                         prop="sourceOrderUnCancelAmount" label="未核销金额"
        />
        <el-table-column  :render-header="crud.renderHeader" v-if="columns.visible('sourceOrderStatus')" prop="sourceOrderStatus"
                         label="单据状态"
        >
          <template slot-scope="{row}">
            {{ row.sourceOrderStatus|sourceOrderStatusFilter(dict.common_order_status) }}
          </template>
        </el-table-column>
        <!-- <el-table-column width="160" :render-header="crud.renderHeader" v-if="columns.visible('balanceAmount')" prop="balanceAmount"
                         label="余额"/> -->
        <el-table-column width="160" :render-header="crud.renderHeader" v-if="columns.visible('remark')" prop="remark"
                         label="备注"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('sourceOrderRemark')" prop="sourceOrderRemark"
                         label="来源单据备注"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('sourceOrderCreateUserName')" prop="sourceOrderCreateUserName"
                         label="来源单据制单人"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('sourceOrderCreateTime')" prop="sourceOrderCreateTime"
                         label="来源单据制单时间"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('sourceOrderAuditUserName')" prop="sourceOrderAuditUserName"
                         label="来源单据审核人"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('sourceOrderAuditTime')" prop="sourceOrderAuditTime"
                         label="来源单据审核时间"/>
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('sCustomerName')" prop="sCustomerName"-->
<!--                         label="所属二批"/>-->
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudFinanceBankBalanceDetail from '@/api/erp/financeBankBalanceDetail'
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

// crud交由presenter持有
const defaultCrud = CRUD({
  title: '银行余额明细',
  query: {
    busDate: null,
    sourceCode: null,
    bankIds: null,
    sourceType: null
  },
  url: 'api/financeBankBalanceDetail',
  sort: 'id,desc',
  crudMethod: {...crudFinanceBankBalanceDetail}
})
const defaultForm = {
  id: null,
  orderId: null,
  busDate: null,
  customerId: null,
  customerCode: null,
  customerName: null,
  sourceType: null,
  sourceId: null,
  sourceCode: null,
  inAmount: null,
  sourceOrderCancelAmount:null,
  sourceOrderUnCancelAmount:null,
  sourceOrderStatus:null,
  outAmount: null,
  balanceAmount: null,
  seq: null,
  remark: null,
  createUserId: null,
  createUserCode: null,
  createUserName: null,
  createTime: null,
  updateUserId: null,
  updateUserCode: null,
  updateUserName: null,
  updateTime: null
}
export default {
  name: 'FinanceBankBalanceDetail',
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['t_finance_bank_balance_detail_source_type','common_order_status'],
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      permission: {
        add: ['admin', 'financeBankBalanceDetail:add'],
        edit: ['admin', 'financeBankBalanceDetail:edit'],
        del: ['admin', 'financeBankBalanceDetail:del'],
        import: ['admin', 'financeBankBalanceDetail:import'],
        export: ['admin', 'financeBankBalanceDetail:export'],
        copy: ['admin', 'financeBankBalanceDetail:copy'],
        upload: ['admin', 'financeBankBalanceDetail:upload'],
        download: ['admin', 'financeBankBalanceDetail:download'],
      },
      rules: {
        orderId: [
          {required: true, message: '单据id不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  filters: {

    sourceOrderStatusFilter(val, list) {
      const item = list.find(el => el.value === val);
      if (item) {
        return item.label;
      } else {
        return '-';
      }
    }
  },
  updated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  watch: {},
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    goToPage(num,code){
      console.log(num)
      if(num == 1){
        this.$router.push({path: '/finance/financeCustomerReceiptDetail/' + code})

      }else if(num == 2){
        this.$router.push({path: '/finance/financeSupplierPaymentDetail/' + code})
      }
      // if (num == 0){
      //   this.$router.push({path: '/sale/salesOrderDetail/' + code})
      // }else {
      //   this.$router.push({path: '/ware/wareSalesReturn/' + code})
      // }
    },
    sumRow({ columns, data }){
      console.log(this.crud)
      const sums = []
      for (let i in  columns) {
        if (['inAmount', 'sourceOrderCancelAmount', 'sourceOrderUnCancelAmount'].includes(columns[i].property)) {
          let selectSum = 0
          this.$refs.table.selection.forEach(item => {
            console.log( item[columns[i].property])
            selectSum += item[columns[i].property]
          })
          sums[i] = <p>{selectSum}
            <hr/>
            {this.crud.summary[columns[i].property] || 0}</p>
        }
        sums[0] = <p>选中
          <hr/>
          合计</p>
      }
      return sums
    },
    getValue(e) {
      this.query.startTime = this.query.dataExtent[0]
      this.query.endTime = this.query.dataExtent[1]
    },
  }
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
